package com.dsl.dslsuzhou.mapper;

import com.dsl.dslsuzhou.engine.BuiltSql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * JDBC执行器
 */
@Component
public class JdbcExecutor {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    /**
     * 执行查询SQL
     */
    public List<Map<String, Object>> executeQuery(BuiltSql builtSql) {
        return jdbcTemplate.queryForList(builtSql.getSql(), builtSql.getParameters().toArray());
    }
    
    /**
     * 执行存储过程
     */
    public List<Map<String, Object>> callStoredProcedure(String procedureName, Object... params) {
        StringBuilder sql = new StringBuilder("CALL ");
        sql.append(procedureName).append("(");
        
        for (int i = 0; i < params.length; i++) {
            if (i > 0) sql.append(", ");
            sql.append("?");
        }
        sql.append(")");
        
        return jdbcTemplate.queryForList(sql.toString(), params);
    }
}
